package com.tsu.mall.admin.controller;



import com.tsu.mall.admin.entity.Attendant;
import com.tsu.mall.common.VO.IndexTableBingVO;
import com.tsu.mall.common.VO.IndexTableVO;
import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.VO.ResultVO;
import com.tsu.mall.common.entity.Attachment;
import com.tsu.mall.common.entity.Wares;
import com.tsu.mall.common.entity.Waresclassify;
import com.tsu.mall.common.service.IWaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wares")
public class WaresController {

    @Autowired
    private IWaresService iWaresService;
    @GetMapping("/waresPage")
    public String waresPage(){
        return "wares";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO waresList(Integer page, Integer limit, String keyword,String waresStatus) {
        return iWaresService.doList(page,limit,keyword,waresStatus);
    }
//查询当前id对应的所有分类信息
    @PostMapping("/child")
    @ResponseBody
    public List<Waresclassify> children(Integer id){
        return iWaresService.selectChildren(id);
    }

    @PostMapping("/selectById")
    @ResponseBody
    public LayuiTableVO selectById(Integer id){
        return iWaresService.selectById(id);
    }

    @PostMapping("/mainPic")
    @ResponseBody
    public LayuiTableVO mainPic(MultipartFile file){
        String fileName=System.currentTimeMillis()+".jpg";
        File f=new File("D:/test/"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LayuiTableVO vo=new LayuiTableVO();

        String s="/upload/"+fileName;
        Map<String ,String> map=new HashMap<>();
        map.put("mainPic",s);
        vo.setCode(0);
        vo.setData(map);
        return vo;
    }
    @PostMapping("/save")
    @ResponseBody
    public ResultVO save(Wares wares){
        return iWaresService.doSave(wares);
    }


    @RequestMapping ("/deleteWares")
    @ResponseBody
    public void deleteAttendants(@RequestParam("ids[]") Integer[] ids){
        iWaresService.deleteWares(ids);
    }

    @RequestMapping ("/upShelf")
    @ResponseBody
    public void upShelfWares(@RequestParam("ids[]") Integer[] ids){
        iWaresService.upShelfWares(ids);
    }

    @RequestMapping ("/downShelf")
    @ResponseBody
    public void downShelfWares(@RequestParam("ids[]") Integer[] ids){
        iWaresService.downShelfWares(ids);
    }

    //多附件上传接口
    @PostMapping("/attachments")
    @ResponseBody
    public List<Attachment> uploadAttachments(String ids,Integer id){
        iWaresService.uploadAttachmentByIds(ids,id);
        return iWaresService.selectByids(ids);
    }

    @GetMapping("/selectAvatars")
    @ResponseBody
    public List<Attachment> selectAvatar(Integer pid){
        return iWaresService.selectList(pid,"wares");
    }

    @PostMapping("/dateZhu")
    @ResponseBody
    public IndexTableVO dateZhu(){
        IndexTableVO vo=new IndexTableVO();
        //查询商品销量前五的商品的信息
        List<Wares> list=iWaresService.selectSellTop5Wares();
        String[] dataZhuX= new String[list.size()];
        Integer[] dataZhuStore= new Integer[list.size()];
        for(int i=0;i<list.size(); i++){
            if (list.get(i).getWaresName()!=null&& !"".equals(list.get(i).getWaresName())){
                dataZhuX[i]=list.get(i).getWaresName();
            }
            if (list.get(i).getStore()!=null&& !"".equals(list.get(i).getStore())){
                dataZhuStore[i]=list.get(i).getStore();
            }
        }
        vo.setX(dataZhuX);
        vo.setStore(dataZhuStore);
        return vo;
    }
    @PostMapping("/dateBing")
    @ResponseBody
    public List<IndexTableBingVO> dateBing(){
    //查询饼状图展示所需要的数据，比如商品名称还有销量
        return iWaresService.dateBing();
    }
}
